package sicnu.cs.ich.log.mq;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.api.domain.entity.LoginLog;
import sicnu.cs.ich.api.domain.entity.OperationLog;
import sicnu.cs.ich.log.service.ILogService;
import sicnu.cs.ich.rocketmq.rocketmq.AbstractRocketConsumer;

/**
 * @author CaiKe
 * @date 2021/12/7 18:38
 */
@Slf4j
@Component
@AllArgsConstructor
public class LogConsumer extends AbstractRocketConsumer {

    private final ILogService logService;

    @Override
    public void init() {
        super.necessary("log", "*", "日志记录消费者");
        handleMessage();
    }

    /**
     * 处理操作日志
     * @param message
     */
    public ConsumeConcurrentlyStatus oper(MessageExt message) throws InterruptedException {
        final OperationLog operLog = JSON.parseObject(message.getBody(), OperationLog.class);
        logService.tempStoreOperationLog(operLog);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    /**
     * 处理登录日志
     * @param message
     */
    public ConsumeConcurrentlyStatus login(MessageExt message) throws InterruptedException {
        final LoginLog loginLog = JSON.parseObject(message.getBody(), LoginLog.class);
        logService.tempStoreLoginLog(loginLog);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}